package controllers

import (
	"exam-goods/models"
	beego "github.com/beego/beego/v2/server/web"
	"gorm.io/gorm"
)

type GoodsController struct {
	beego.Controller
}

func (c *GoodsController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	p, _ := c.GetFloat("price")
	k, _ := c.GetFloat("kg")
	s, _ := c.GetFloat("stock")
	g := models.Good{
		Name:  c.GetString("name"),
		Price: p,
		Kg:    k,
		Stock: s,
	}
	if g.Kg == 0 {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "重量不得为空",
		}
		c.ServeJSON()
		return
	}
	if g.Price == 0 {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "价格不得为空",
		}
		c.ServeJSON()
		return
	}
	if g.Stock == 0 {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "存量不得为空",
		}
		c.ServeJSON()
		return
	}
	if g.Name == "" {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "商品名不得为空",
		}
		c.ServeJSON()
		return
	}
	err := models.DB.Create(&g).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "商品添加成功",
	}
	c.ServeJSON()
}
func (c *GoodsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	limit, _ := c.GetInt("limit", 10)
	page, _ := c.GetInt("page", 1)
	name := c.GetString("name")
	var goods []models.Good
	sql := models.DB.Limit(limit).Offset((page - 1) * limit)
	if name != "" {
		sql = sql.Where("name like ?", "%"+name+"%")
	}
	err := sql.Find(&goods).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	var total int
	models.DB.Model(&models.Good{}).Pluck("count(*)", &total)
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "获取成功",
		Data: struct {
			Total int
			Goods []models.Good
		}{
			total,
			goods,
		},
	}
	c.ServeJSON()
}
func (c *GoodsController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	if c.Ctx.Input.Method() == "GET" {
		g := models.Good{}
		models.DB.Where("id = ?", id).First(&g)
		c.Data["json"] = models.Msg{
			Data: g,
		}
		c.ServeJSON()
		return
	}
	p, _ := c.GetFloat("price")
	k, _ := c.GetFloat("kg")
	s, _ := c.GetFloat("stock")
	g := models.Good{
		Model: gorm.Model{ID: uint(id)},
		Name:  c.GetString("name"),
		Price: p,
		Kg:    k,
		Stock: s,
	}
	err := models.DB.Updates(&g).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "修改成功",
	}
	c.ServeJSON()
	return
}
